package com.atguigu.edu.controller;


import com.atguigu.base.ExceptionHnadler.CustomException;
import com.atguigu.edu.service.VideoService;
import com.atguigu.edu.client.VodClientService;
import com.atguigu.edu.entity.Video;
import com.atguigu.utils.R;
import io.swagger.annotations.Api;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author ld
 * @since 2022-08-16
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
@Api(description = "小节信息管理")
public class VideoController {
    @Resource
    private VideoService videoService;
    @Resource
    private VodClientService vodClientService;
    //添加小节
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody Video video){
        videoService.save(video);
        return R.ok();
    }
    //根据id获取小节信息
    @GetMapping("/getVideo/{id}")
    public R getVideo(@PathVariable String id){
        Video video = videoService.getById(id);
        return R.ok().data("video",video);
    }
    //修改小节信息
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody Video video){
        videoService.updateById(video);
        return R.ok();
    }
    //先删除视频信息,再删除小节信息
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id){
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            R result = vodClientService.deleteVideo(videoSourceId);
            if(result.getCode()==20001){
                throw  new CustomException(20001,"删除视频失败,熔断器......");
            }
        }
        boolean b = videoService.removeById(id);
        if(b){
            return R.ok();
        }else{
            return R.error().data("message","删除小节失败");
        }

    }
}

